package gui;

import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.Font;

public class ConnectionPanel extends JPanel {
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	private JButton btnConnect;

	public ConnectionPanel() {
		try {
			// Set System L&F
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (UnsupportedLookAndFeelException e) {
			System.out.println("Look and Feel unsupported");

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			System.exit(1);
		} catch (InstantiationException e) {
			e.printStackTrace();
			System.exit(1);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			System.exit(1);
		}

		setSize(443, 200);

		JLabel lblCameraHttp = new JLabel("IP Adress:");

		JLabel lblCameraPort = new JLabel("Command Port:");

		JLabel lblCameraMotionPort_1 = new JLabel("Motion Port:");

		textField = new JTextField("localhost");
		textField.setColumns(10);

		textField_1 = new JTextField("8001");
		textField_1.setColumns(10);

		textField_2 = new JTextField("8002");
		textField_2.setColumns(10);

		JLabel lblCameraPort_1 = new JLabel("Camera 1");
		lblCameraPort_1.setFont(new Font("Tahoma", Font.BOLD, 13));

		btnConnect = new JButton("Connect");

		JLabel lblCameraHttp_1 = new JLabel("Camera 2");
		lblCameraHttp_1.setFont(new Font("Tahoma", Font.BOLD, 13));

		textField_5 = new JTextField("8005");
		textField_5.setColumns(10);

		textField_4 = new JTextField("8004");
		textField_4.setColumns(10);

		textField_3 = new JTextField("localhost");
		textField_3.setColumns(10);
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
					.addGap(29)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(btnConnect)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
								.addComponent(lblCameraHttp)
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
									.addComponent(lblCameraMotionPort_1)
									.addComponent(lblCameraPort)))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
									.addGap(18)
									.addComponent(textField_4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
									.addGap(18)
									.addComponent(textField_5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
								.addGroup(groupLayout.createSequentialGroup()
									.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
										.addComponent(lblCameraPort_1)
										.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
									.addGap(18)
									.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
										.addComponent(lblCameraHttp_1)
										.addComponent(textField_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))))
					.addContainerGap(85, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(33)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(lblCameraHttp_1)
						.addComponent(lblCameraPort_1))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblCameraHttp)
						.addComponent(textField_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(8)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblCameraPort)
						.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(textField_4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblCameraMotionPort_1)
						.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(textField_5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(btnConnect)
					.addContainerGap(37, Short.MAX_VALUE))
		);
		setLayout(groupLayout);
		setVisible(true);

	}

	public void addConnectListener(ActionListener connectionListener) {
		btnConnect.addActionListener(connectionListener);
	}
/**
 * Collects the values from the textfields
 * @param camOne input vektor for camera one
 * @param camTwo input vektor for camera two
 */
	public void getTextValues(String[] camOne, String[] camTwo) {
		camOne[0] = textField.getText();
		camOne[1] = textField_1.getText();
		camOne[2] = textField_2.getText();
		camTwo[0] = textField_3.getText();
		camTwo[1] = textField_4.getText();
		camTwo[2] = textField_5.getText();
	}
	/**
	 * Sets the text on the connect button to Disconnect
	 */
	public void setDisconnect(){
		btnConnect.setText("Disconnect");
	}
	/**
	 * Sets the text on the connect button to Connect
	 */
	public void setConnect(){
		btnConnect.setText("Connect");
	}
	/**
	 * Checks if the gui is in connected mode
	 * @return true if connected
	 */
	public boolean connected() {
		return btnConnect.getText() == "Disconnect";
	}
}
